package com.ymttest.database.sqlwapper;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shop2cn.iapi.sqpayment.parameter.PaymentMessageDto;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.model.FqlCreditApply;
import com.ymttest.database.model.FqlCreditApplyExample;
import com.ymttest.database.model.FqlMember;
import com.ymttest.database.model.FqlMemberExample;
import com.ymttest.database.model.MCmbaggrement;
import com.ymttest.database.model.RefundMiscRequestLog;
import com.ymttest.database.model.payment.Paychannel;
import com.ymttest.database.model.MRefund;
import com.ymttest.database.model.PaymentCheck;
import com.ymttest.database.model.PaymentCheckDetail;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name :DefaultWapper.java
 * @Author : zhouyi
 * @Date : 2015年6月1日
 * @Copyright : 洋码头
 ************************************************************************************/
public class SQPayGateWayWapper {
	static SqlManager manage;

	public SQPayGateWayWapper() {
		manage = new SqlManager("paygateway");
	}
	/**
	 * 查询退款申请
	 * 
	 * @param paymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<MRefund> selectRefundRequestByOrderid(String orderid) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("orderid", orderid);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByOrderid", m);
		manage.commit();
		return pia;
	}
	/**
	 * 查询退款信息
	 * 
	 * @param pid
	 *            PaymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<MRefund> selectRefundRequestByPaymentId(String pid) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("pid", pid);
		manage.commit();
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByPaymentId", m);
		manage.commit();
		return pia;
	}
	public List<PaymentMessageDto> selectPaymentMessageDtoByPids(List<String> pids) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<PaymentMessageDto> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaymentMessageDtoByPids2", pids);
		manage.commit();
		return pia;
	}
	/**
	 * 查询退款申请单
	 * 
	 * @param paymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<MRefund> selectRefundRequestByTradeNo(String tradingid) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("tradeNo", tradingid);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByTradeNo", m);
		manage.commit();
		return pia;
	}
	/**
	 * RefundBatchNo查询退款记录
	 * 
	 * @param rno
	 * @return
	 * @throws InterruptedException
	 */
	public List<MRefund> selectRefundRequestByRefundBatchNo(String rno) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("rno", rno);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByRefundBatchNo", m);
		manage.commit();
		return pia;
	}

	public List<MRefund> selectRefundRequestByRefundId(String rid) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("rid", rid);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByRefundId", m);
		manage.commit();
		return pia;
	}
	/**
	 * 查询支付单
	 * 
	 * @param paymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<MPayment> selectPPPaymentByPaymentId(String paymentId) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("paymentId", paymentId);
		List<MPayment> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPPPaymentByPaymentId", m);
		manage.commit();
		return pia;
	}
	//selectMpaysynclogByOrderNo
	/**
	 * 查询mpay同步记录
	 * @param tradingid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Map> selectMpaysynclogByOrderNo(String orderNo) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("orderNo", orderNo);
		List<Map> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectMpaysynclogByOrderNo", m);
		manage.commit();
		return pia;
	}
	/**
	 * 查询支付单
	 * 
	 * @param paymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<MPayment> selectPaymentByTradingid(String tradingid) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("tradingid", tradingid);
		List<MPayment> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaymentByTradingid", m);
		manage.commit();
		return pia;
	}

	public List<MCmbaggrement> selectCmbAggrementByUId(String uid) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("uid", uid);
		List<MCmbaggrement> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectCmbAggrementByUId", m);
		manage.commit();
		return pia;
	}
	//selectPaychannel
	/**
	 * 查询商户的支付渠道
	 * @param mid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Paychannel> selectPaychannel(Integer mid) throws InterruptedException {
		Thread.sleep(1000);
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("mid", mid);
		List<Paychannel> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaychannel", m);
		manage.commit();
		if(pia.size()==0) {
			Thread.sleep(1000);
			pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaychannel", m);
			manage.commit();
		}
		return pia;
	}
	public List<Map> selectCmbpublickey() throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<Map> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectCmbpublickey", null);
		manage.commit();
		return pia;
	}
	/**
	 * 删除签约记录
	 * 
	 * @param uid
	 * @return
	 * @throws InterruptedException
	 */
	public int deleteCmbAggrementByUId(String uid) throws InterruptedException {
		Logger.comment("uid：" + uid);
		int i = 0;
		if (uid != null && uid.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("uid", uid);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.deleteCmbAggrementByUId", m);
			manage.commit();
		}
		return i;
	}

	public List<MRefund> selectRefundRequestByPaymentid(String paymentid) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("paymentid", paymentid);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByPaymentid", m);
		manage.commit();
		return pia;
	}
	/**
	 * @param tid getRefundNo
	 * @return
	 * @throws InterruptedException
	 */
	public List<MRefund> selectRefundRequestByTraceId(String tid) throws InterruptedException {
		Map m = new HashMap<String, String>();
		m.put("tid", tid);
		List<MRefund> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectRefundRequestByTraceId", m);
		manage.commit();
		return pia;
	}
	/**
	 * 更新退款状态
	 * 
	 * @param refundno
	 * @param status
	 * @return
	 * @throws InterruptedException
	 */
	public int updateRefundStatus(String refundno, String status) throws InterruptedException {
		Logger.comment("refundbatchno：" + refundno + "更新退款状态为：" + status);
		int i = 0;
		if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("refundno", refundno);
			m.put("status", status);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateRefundStatus", m);
			manage.commit();
		}
		return i;
	}
	/**
	 * 更新退款优惠金额
	 * @param refundno
	 * @param rdamt
	 * @return
	 * @throws InterruptedException
	 */
	public int updateRefundRefundDiscountAmt(String refundno, BigDecimal rdamt) throws InterruptedException {
		Logger.comment("refundbatchno：" + refundno + "更新退款优惠金额：" + rdamt);
		int i = 0;
		if (refundno != null && refundno.length() > 0 && rdamt != null) {
			Map m = new HashMap<String, String>();
			m.put("refundno", refundno);
			m.put("rdamt", rdamt);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateRefundRefundDiscountAmt", m);
			manage.commit();
		}
		return i;
	}
	public int updatePayTimeByPid(String pid, String date) throws InterruptedException {
		Logger.comment("updatePayTimeByPid：" + pid + "更新时间为：" + date);
		int i = 0;
		if (pid != null && pid.length() > 0 && date != null && date.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			m.put("date", date);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updatePayTimeByPid", m);
			manage.commit();
		}
		return i;
	}
	
	/**
	 * 更新 InstProxyPaymentId 为null
	 * 用于微信APP小程序支付异步回调发送mq异常情况
	 * @param pid
	 * @return
	 * @throws InterruptedException
	 */
	public int updateInstProxyPaymentIdNull(String pid) throws InterruptedException {
		Logger.comment("updateInstProxyPaymentIdNull：" + pid );
		int i = 0;
		if (pid != null && pid.length() > 0 ) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateInstProxyPaymentIdNull", m);
			manage.commit();
		}
		return i;
	}
	/**
	 * mock未迁库
	 * @param mockid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Map> selectMockHttpRecordByMockId(String mockid) throws InterruptedException {
		return new PaymentDefaultWapper().selectMockHttpRecordByMockId(mockid);
	}

	/**
	 * @param pid
	 *            paymentid
	 * @param status
	 *            PayStatus
	 * @return
	 * @throws InterruptedException
	 */
	public int updateNotifyStatus(String pid, String status) throws InterruptedException {
		Logger.comment("paymentid：" + pid + "更新第三方异步通知状态为：" + status);
		int i = 0;
		if (pid != null && pid.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			m.put("status", status);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateNotifyStatus", m);
			manage.commit();
		}
		return i;
	}

	/**
	 * @param pid
	 *            paymentid
	 * @param status
	 *            PayStatus
	 * @return
	 * @throws InterruptedException
	 */
	public int updatePayStatus(String pid, String status) throws InterruptedException {
		Logger.comment("paymentid：" + pid + "更新支付状态为：" + status);
		int i = 0;
		if (pid != null && pid.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			m.put("status", status);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updatePayStatus", m);
			manage.commit();
		}
		return i;
	}
	public int updateCheckStatus(String pid, String status) throws InterruptedException {
		Logger.comment("paymentid：" + pid + "更新支付状态为：" + status);
		int i = 0;
		if (pid != null && pid.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			m.put("status", status);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateCheckStatus", m);
			manage.commit();
		}
		return i;
	}
	public int updateRefundSoftDeleteFlag(String refundno, String status) throws InterruptedException {
		Logger.comment("refundbatchno：" + refundno + "更新审核状态为：" + status);
		int i = 0;
		if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("refundno", refundno);
			if(status=="1"){
				m.put("status", true);
			}else{
				m.put("status", false);
			}
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateRefundSoftDeleteFlag", m);
			manage.commit();
		}
		return i;
	}
	/**
	 * 更新退款状态
	 * 
	 * @param refundno
	 * @param status
	 * @return
	 * @throws InterruptedException
	 */
	public int updateRefundApproveStatus(String refundno, String status) throws InterruptedException {
		Logger.comment("refundbatchno：" + refundno + "更新退款审核状态为：" + status);
		int i = 0;
		if (refundno != null && refundno.length() > 0 && status != null && status.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("refundno", refundno);
			m.put("status", status);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateRefundApproveStatus", m);
			manage.commit();
		}
		return i;
	}
	/**
	 * 查询accounting信息
	 * 
	 * @param {bid=RefundBatchNo}
	 * @return
	 */
	public List<Map> selectPPAccoutingLogByBizno(String bid) throws InterruptedException {
		return new YmtLoggingWapper().selectPPAccoutingLogByBizno(bid);
	}

	public List<Map> selectRefundMiscRequestLogByRefundId(String refundid) throws InterruptedException {
		return new YmtLoggingWapper().selectRefundMiscRequestLogByRefundId(refundid);
	}
	/**
	 * 更新RefundNo
	 * 
	 * @param rno
	 * @param pid
	 * @return
	 * @throws InterruptedException
	 */
	public int updateRefundNo(String rno, String pid) throws InterruptedException {
		int i = 0;
		if (rno != null && rno.length() > 0 && pid != null && pid.length() > 0) {
			Map m = new HashMap<String, String>();
			m.put("pid", pid);
			m.put("rno", rno);
			i = manage.update("com.ymttest.database.mapping.PayGateWayMapper.updateRefundNo", m);
			manage.commit();
		}
		return i;
	}
//	selectFqlCreditApplyByExample
	public List<FqlCreditApply> selectFqlCreditApplyByUserId(int userId) {
		FqlCreditApplyExample example=new FqlCreditApplyExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<FqlCreditApply> list = manage
				.getSelectResults(
						"com.ymttest.database.mapping.PayGateWayMapper.selectFqlCreditApplyByExample",
						example);
		return list;

	}
	/**
	 * 查询退单
	 * 
	 * @param orderId
	 * @return
	 */
	public List<Map> queryRefundrequestByApproveStatus(int approveStatus) {
		List<Map> list = manage
				.getSelectResults(
						"com.ymttest.database.mapping.PayGateWayMapper.queryRefundrequestByApproveStatus",
						approveStatus);
		return list;

	}

	/**
	 * 查询退单
	 * 
	 * @param orderId
	 * @return
	 */
	public List<Map> queryRefundrequestByApproveStatusAndRefundStatus(
			int approveStatus, int refundStatus) {
		Map m = new HashMap();
		m.put("approveStatus", approveStatus);
		m.put("refundStatus", refundStatus);
		List<Map> list = manage
				.getSelectResults(
						"com.ymttest.database.mapping.PayGateWayMapper.queryRefundrequestByApproveStatusAndRefundStatus",
						m);
		return list;

	}
	/**
	 * 查询对账单明细
	 * @param InstPaymentId
	 * @return
	 * @throws InterruptedException
	 */
	public List<PaymentCheck> selectPaymentCheck(String paytype,String fname) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("paytype", paytype);
		m.put("fname", fname);
		Logger.debug("selectPaymentCheck:"+m);
		manage.commit();
		List<PaymentCheck> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaymentCheck", m);
		manage.commit();
		return pia;
	}
	public List<PaymentCheckDetail> selectPaymentCheckDetail(Integer PaymentCheckId,String InstPaymentId) throws InterruptedException {
		@SuppressWarnings("unchecked")
		Map m = new HashMap<String, String>();
		m.put("InstPaymentId", InstPaymentId);
		m.put("PaymentCheckId", PaymentCheckId);
		Logger.debug("selectPaymentCheckDetail:"+m);
		manage.commit();
		List<PaymentCheckDetail> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPaymentCheckDetail", m);
		manage.commit();
		return pia;
	}
	
	/**
	 * 查询FqlMember
	 * @param example
	 * @return
	 * @throws InterruptedException
	 */
	public List<FqlMember> selectFqlMemberExample(FqlMemberExample example) throws InterruptedException {
		@SuppressWarnings("unchecked")
		List<FqlMember> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectFqlMemberExample", example);
		manage.commit();
		return pia;
	}
//
//
//
//
//
//	/**
//	 * 查询发货信息
//	 * 
//	 * @param pid
//	 *            PaymentId
//	 * @return
//	 * @throws InterruptedException
//	 */
//	public List<Map> selectCompensateProcessInfoByPaymentId(String pid) throws InterruptedException {
//		@SuppressWarnings("unchecked")
//		Map m = new HashMap<String, String>();
//		m.put("pid", pid);
//		List<Map> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectCompensateProcessInfoByPaymentId", m);
//		manage.commit();
//		return pia;
//	}
//

//
//
//	/**
//	 * 查询退款申请
//	 * 
//	 * @param paymentId
//	 * @return
//	 * @throws InterruptedException
//	 */
//	public List<RefundMiscRequestLog> selectRefundMiscRequestLogByRefundBatchNo(String rno) throws InterruptedException {
//		Map m = new HashMap<String, String>();
//		m.put("rno", rno);
//		List<RefundMiscRequestLog> pia = manage.getSelectResults("com.ymttest.database.mapping.RefundMiscRequestLogMapper.selectRefundMiscRequestLogByRefundBatchNo", m);
//		manage.commit();
//		return pia;
//	}
//
//	
//
//	
//
//




//
//	public List<MPayment> selectPPPaymentByTradingId(String tradid) throws InterruptedException {
//		Map m = new HashMap<String, String>();
//		m.put("tradid", tradid);
//		List<MPayment> pia = manage.getSelectResults("com.ymttest.database.mapping.PayGateWayMapper.selectPPPaymentByTradingId", m);
//		manage.commit();
//		return pia;
//	}
//

	public static void main(String args[]) {
		try {
//			List<String> pids=Arrays.asList("18121011241316598");
//			Logger.debug(FormatUtil.beanToGSONString(new PayGateWayWapper().selectPaymentMessageDtoByPids(pids).get(0)));
			
			Logger.debug(FormatUtil.beanToGSONString(new SQPayGateWayWapper().selectPaychannel(3383)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
